package com.learn.iot.register;

import com.learn.iot.bean.MyMapperFactoryBean;
import org.springframework.beans.factory.support.*;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 利用这个类将mybatis的对象注入到beanDefinitionMap中
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		/**通过BeanDefinitionBuilder构建一个MyMapperFactoryBean的beanDefinition*/
		BeanDefinitionBuilder beanDefinitionBuilder =
				BeanDefinitionBuilder.genericBeanDefinition(MyMapperFactoryBean.class);
		GenericBeanDefinition beanDefinition = (GenericBeanDefinition) beanDefinitionBuilder.getBeanDefinition();
		/**给MyMapperFactoryBean的构造方法传参*/
		beanDefinition.getConstructorArgumentValues().addGenericArgumentValue("com.learn.iot.mapper.CityMapper");
		/**将beanDefinition加到beanDefinitionMap*/
		registry.registerBeanDefinition("mapper",beanDefinition);
	}

}
